package com.mark.web.sys.controller.frontend;

import com.github.pagehelper.PageInfo;
import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.web.common.service.IUploadFilesService;
import com.mark.web.sys.entity.vo.UserImgVo;
import com.mark.web.sys.service.IUserImgService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户头像控制器
 * @Author: Mark
 * @CreateDate: 2019/1/19 21:05
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@RestController
@RequestMapping("/sys/user/img/")
public class UserImgController {

    private final IUserImgService iUserImgService;
    private final IUploadFilesService iUploadFilesService;

    @Autowired
    public UserImgController(IUserImgService iUserImgService, IUploadFilesService iUploadFilesService) {
        this.iUserImgService = iUserImgService;
        this.iUploadFilesService = iUploadFilesService;
    }

    @ApiOperation(value = "上传用户头像")
    @SuppressWarnings("unchecked")
    @PostMapping(value = "uploadImage")
    public ServerResponse<Map<String,String>> uploadFile(@RequestParam(value = "files", required = false) MultipartFile[] files, ServletRequest request) {
        //定义模块名为用户模块
        final String modules = "user";
        //模块id为用户id
        String moduleId = Integer.toString(CurrentUser.getCurrentUserId());

        ServerResponse response = iUploadFilesService.uploadFiles(files, modules, moduleId);
        List<Map<String, String>> result  = (List<Map<String, String>>) response.getData();
        Map<String, String> urlMap = result.get(0);
        return ServerResponse.createBySuccess(urlMap);
    }

    @ApiOperation(value = "新增用户头像")
    @ApiImplicitParam(name = "imageUrl",value = "用户头像地址")
    @PostMapping(value = "add.do")
    public ServerResponse<String> addImage(String imageUrl) {
        if(StringUtils.isBlank(imageUrl)){
            return ServerResponse.createByIllegalArgument("输入的用户头像地址不能为空！");
        }
        Integer currentUserId = CurrentUser.getCurrentUserId();
        return iUserImgService.addImage(imageUrl,currentUserId);
    }

    /**
     *获取用户所有的头像
     * @param pageNum：当前的页数
     * @param pageSize：页面的大小
     * @return ：当前用户的头像列表信息
     */
    @ApiOperation(value = "获取所有的头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前的页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",defaultValue = "4")
    })
    @PostMapping(value = "get_images.do")
    public ServerResponse<PageInfo<UserImgVo>> getUserImgList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                              @RequestParam(value = "pageSize",defaultValue = "4") int pageSize){
        int currentUserId = CurrentUser.getCurrentUserId();
        return iUserImgService.getUserImageList(pageNum,pageSize,currentUserId);
    }

}
